package access

import (
	"iissy.com/src/models"
	"iissy.com/src/utils"
	"math"
)

// RuleEngine is yes.
func RuleList(page int, size int) (*models.RuleEngines, error) {
	var result models.RuleEngines
	result.Items = []*models.RuleEngine{}

	rows, err := db.Query("select id,rule_name,rule_desc,is_deleted,create_id,created_at,updated_at from rule_engine where id= 1 order by created_at desc limit ?, ?", (page-1)*size, size)
	utils.CheckErr(err)

	for rows.Next() {
		item := models.RuleEngine{}
		err = rows.Scan(&item.ID, &item.RuleName, &item.RuleDesc, &item.IsDeleted, &item.CreateId, &item.CreateAt, &item.UpdateAt)
		utils.CheckErr(err)

		result.Items = append(result.Items, &item)
	}

	total := 0
	err = db.QueryRow("select count(*) from rule_engine ").Scan(&total)
	utils.CheckErr(err)

	pageCount := int(math.Ceil(float64(total) / float64(size)))
	result.PageArgs = models.PageArgs{PageNumber: page, TotalCount: total, PageSize: size, PageCount: pageCount}
	rows.Close()
	return &result, nil
}

// Detail is for RuleEngine
func RuleEngineDetail(id string) (*models.RuleEngine, error) {
	var result models.RuleEngine

	err := db.QueryRow("select id,rule_name,rule_desc,is_deleted,create_id,created_at,updated_at from rule_engine where id= ?", id).Scan(&result.ID, &result.RuleName, &result.RuleDesc, &result.IsDeleted, &result.CreateId, &result.CreateAt, &result.UpdateAt)
	utils.CheckErr(err)

	list, err := db.Query("select id,rule_name,rule_desc,is_deleted,create_id,created_at,updated_at from rule_engine where created_at < ? order by created_at desc limit 10", result.CreateAt)
	utils.CheckErr(err)
	result.List = []*models.RuleEngine{}
	for list.Next() {
		var simple models.RuleEngine
		err = list.Scan(&simple.ID, &simple.RuleName, &simple.RuleDesc, &simple.IsDeleted, &simple.CreateId, &simple.CreateAt, &simple.UpdateAt)
		utils.CheckErr(err)

		result.List = append(result.List, &simple)
	}

	db.Exec("update rule_engine set is_deleted = 1 where id = ?", id)

	list.Close()
	return &result, nil
}

// Post is yes
func RuleEnginePost(ruleEngine models.RuleEngine) (bool, error) {
	res, err := db.Exec("insert rule_engine set id=?,rule_name=?,rule_desc=?,is_deleted=?,create_id=?,created_at=?,updated_at=?", ruleEngine.ID, ruleEngine.RuleName, ruleEngine.RuleDesc, ruleEngine.IsDeleted, ruleEngine.CreateId, ruleEngine.CreateAt, ruleEngine.UpdateAt)
	utils.CheckErr(err)

	result, err := res.RowsAffected()
	utils.CheckErr(err)

	return result > 0, nil
}

// GetArticle is for article
func GetRuleEngine(id string) (*models.RuleEngine, error) {
	var result models.RuleEngine

	err := db.QueryRow("select id,rule_name,rule_desc,is_deleted,create_id,created_at,updated_at from rule_engine where id = ?", id).Scan(&result.ID, &result.RuleName, &result.RuleDesc, &result.IsDeleted, &result.CreateId, &result.CreateAt, &result.UpdateAt)
	utils.CheckErr(err)
	return &result, nil
}

// Update is yes
func UpdateRuleEngine(ruleEngine models.RuleEngine) (bool, error) {
	res, err := db.Exec("update rule_engine set rule_name=?,rule_desc=?,is_deleted=?,create_id=? where id=? ", ruleEngine.RuleName, ruleEngine.RuleDesc, ruleEngine.IsDeleted, ruleEngine.CreateId)
	utils.CheckErr(err)

	result, err := res.RowsAffected()
	utils.CheckErr(err)

	return result > 0, nil
}

// DelArticle is yes
func DelRuleEngine(uid int, id string) (bool, error) {
	res, err := db.Exec("delete from rule_engine where id=? ", id)
	utils.CheckErr(err)

	result, err := res.RowsAffected()
	utils.CheckErr(err)

	return result > 0, nil
}
